//
//  CompanyAuViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/9/18.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "CompanyAuViewController.h"
#import "RentVieitor.h"
#import "UploadFileViewController.h"
#import "WaitReviewViewController.h"

@interface CompanyAuViewController ()<UIScrollViewDelegate,UITextFieldDelegate>

@end

@implementation CompanyAuViewController {
    
    UIScrollView *scrollView;
    NSString *fileImg; //存储回调回来的图片
    NSString *companyImg;////存储回调回来的公司授权证明
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.hidden = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BackgroundColor;
    [self creatLeftView];
    
    
}

- (void)creatLeftView {
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 120)];
    headView.backgroundColor = [Utils colorConvertFromString:@"#1fae0c"];
    [self.view addSubview:headView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, KWidth-200, 40)];
    label.text = @"经纪公司认证";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:17];
    label.textColor = [UIColor whiteColor];
    [headView addSubview:label];
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(15, 20+12, 100,30);
    [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 10, 90)];
    [leftBtn setImage:[UIImage imageNamed:@"箭头左-left"] forState:UIControlStateHighlighted];
    [leftBtn setImage:[UIImage imageNamed:@"箭头左-left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:leftBtn];
    
    // 自定义导航栏左侧按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(KWidth-65, 20+10, 50, 20);
    [rightBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:rightBtn];
    
    UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, KWidth, KWidth*90/648)];
    imgview.image = [UIImage imageNamed:@"second-bd"];
    [headView addSubview:imgview];
    
    UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 140, KWidth, 20)];
    firstLabel.text = @"第二步  认证信息";
    firstLabel.textAlignment = NSTextAlignmentCenter;
    firstLabel.font = [UIFont systemFontOfSize:17];
    firstLabel.textColor = [Utils colorConvertFromString:@"#101010"];
    [self.view addSubview:firstLabel];
    
    UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 170, KWidth, 13)];
    secondLabel.text = @"请仔细编辑相关信息";
    secondLabel.textAlignment = NSTextAlignmentCenter;
    secondLabel.font = [UIFont systemFontOfSize:13];
    secondLabel.textColor = [Utils colorConvertFromString:@"#666666"];
    [self.view addSubview:secondLabel];
    
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 200, KWidth, KHeight-200)];
    scrollView.backgroundColor = BackgroundColor;
    scrollView.contentSize = CGSizeMake(KWidth, 510+271);
    scrollView.delegate = self;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    
    NSArray *titleArray = @[@"公司名",@"法人姓名",@"公司注册地址",@"营业执照号码",@"营业执照上传",@"您的姓名",@"联系方式",@"身份证号",@"公司授权证明"];
//    NSArray *textArray = @[@"优联互通",@"吴桂强",@"海王银河科技大厦",@"123456789098",@"",@"廖湘鹏",@"13786210735",@"43252419940417121",@""];
    //选项视图
    CGFloat heigt = 50;
    for (int i=0; i<titleArray.count; i++) {
        
        UIView *selectView = [[UIView alloc] init];
        selectView.backgroundColor = [UIColor whiteColor];
        selectView.tag = 100+i;
        if (i<4 ) {
            
            selectView.frame = CGRectMake(0,heigt*i , KWidth, heigt);
            
        }else  {
            
            selectView.frame = CGRectMake(0, heigt*i+10, KWidth, heigt);
        }
        [scrollView addSubview:selectView];
        
        
        UILabel *selectLabel = [[UILabel alloc] init];
        selectLabel.text = titleArray[i];
        selectLabel.font = [UIFont systemFontOfSize:15];
        selectLabel.textAlignment = NSTextAlignmentLeft;
        selectLabel.textColor = [Utils colorConvertFromString:@"#101010"];
        [selectView addSubview:selectLabel];
        
        CGSize size2 = [@"上传身份证照片件" boundingRectWithSize:CGSizeMake(KWidth, 25) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:selectLabel.font} context:nil].size;
        
        selectLabel.frame = CGRectMake(20, 10, size2.width, 30);
        
        UITextField *textfile = [[UITextField alloc] initWithFrame:CGRectMake(size2.width+30, 10,KWidth-65-size2.width , 30)];
        textfile.tag = 200+i;
        textfile.delegate = self;
//        textfile.text = textArray[i];
        textfile.textColor = [Utils colorConvertFromString:@"#666666"];
        textfile.font = [UIFont systemFontOfSize:13];
        textfile.textAlignment = NSTextAlignmentRight;
        [selectView addSubview:textfile];
        
        if (i==4 ||i==8) {
            
            UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
            selectButton.frame = CGRectMake(KWidth-25, 20, 5, 10);
            [selectButton setBackgroundImage:[UIImage imageNamed:@"箭头-右"] forState:UIControlStateNormal];
            [selectView addSubview:selectButton];
        }
        
        
        
        //分割线
        CALayer *MlineLayer5 = [CALayer layer];
        MlineLayer5.backgroundColor = [[Utils colorConvertFromString:@"#cfcfcf"] CGColor];
        if (i == 3 || i== 9) {
            MlineLayer5.frame = CGRectMake(0 ,heigt-1, KWidth,10);
            MlineLayer5.backgroundColor = [[Utils colorConvertFromString:@"#F0F0F0"] CGColor];
        }else {
            MlineLayer5.frame = CGRectMake(20 ,heigt-1, KWidth-40,1);
        }
        
        [selectView.layer addSublayer:MlineLayer5];
    }
    
    
    
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (textField.tag == 204) {
        
        UploadFileViewController *upVc = [[UploadFileViewController alloc] init];
        upVc.fileImg = fileImg;
        [self.navigationController pushViewController:upVc animated:YES];
        [upVc returnImgBlock:^(NSString *img) {
            
            fileImg = img;
            textField.text = @"已上传(可修改)";
        }];
        return NO;
    }
    
    if (textField.tag == 208) {
        
        UploadFileViewController *upVc = [[UploadFileViewController alloc] init];
        upVc.fileImg = companyImg;
        [self.navigationController pushViewController:upVc animated:YES];
        [upVc returnImgBlock:^(NSString *img) {
            
            companyImg = img;
            textField.text = @"已上传(可修改)";
        }];
        return NO;

    }
    
    return YES;
}

-(void)backAction {

    [self.navigationController popViewControllerAnimated:YES];
}

//提交信息
- (void)nextAction {
    
    
    //公司名字
    UIView *cnameView = (UIView *)[scrollView viewWithTag:100];
    UITextField *cnamefield = (UITextField *)[cnameView viewWithTag:200];
    if (cnamefield.text.length == 0) {
        
        [APPDELEGATE showPro:@"请填写公司名"];
        return;
    }
    
    //法人姓名
    UIView *leperson = (UIView *)[scrollView viewWithTag:101];
    UITextField *leNamefield = (UITextField *)[leperson viewWithTag:201];
    if (leNamefield.text.length == 0) {
        
        [APPDELEGATE showPro:@"填写法人姓名"];
        return;
    }
    
    //公司注册地址
    UIView *locationView = (UIView *)[scrollView viewWithTag:102];
    UITextField *locationfield = (UITextField *)[locationView viewWithTag:202];
    if (locationfield.text.length == 0) {
        
        [APPDELEGATE showPro:@"请填写公司注册地址"];
        return;
    }
    
    //执照号码
   
    UIView *linceView = (UIView *)[scrollView viewWithTag:103];
    UITextField *lincefield = (UITextField *)[linceView viewWithTag:203];
    if (lincefield.text.length == 0) {
        [APPDELEGATE showPro:@"请填写营业执照号码"];
        return;
    }

    //营业执照照片
    if (fileImg == nil || [fileImg isEqualToString:@""]) {
        [APPDELEGATE showPro:@"请上传营业执照照片"];
        return;
    }
    
    //经纪人姓名
    UIView *nameView = (UIView *)[scrollView viewWithTag:105];
    UITextField *namefield = (UITextField *)[nameView viewWithTag:205];
    if (namefield.text.length == 0) {
        [APPDELEGATE showPro:@"请填写您的姓名"];
        return;
    }
    
    //联系方式
    UIView *addressView = (UIView *)[scrollView viewWithTag:106];
    UITextField *tellfield = (UITextField *)[addressView viewWithTag:206];
    if (tellfield.text.length == 0) {
        [APPDELEGATE showPro:@"请填写您的联系方式"];
        return;
    }
    
    //身份证号码
    UIView *cardView = (UIView *)[scrollView viewWithTag:107];
    UITextField *cardfield = (UITextField *)[cardView viewWithTag:207];
    if (cardfield.text.length == 0) {
        [APPDELEGATE showPro:@"请填写身份证号"];
        return;
    }
    
    //营业执照号码
    if (companyImg == nil || [companyImg isEqualToString:@""]) {
        [APPDELEGATE showPro:@"请上传营业执照照片"];
        return;
    }
    
        //    NSData *fileData = UIImageJPEGRepresentation(fileImg, 1.0f);
    NSDictionary *dataDic = User_listsValue;
    NSString *url = [NSString stringWithFormat:@"%@agent/applycompany",Main_Url];
    NSString *number = dataDic[@"number"];
    NSString *token = dataDic[@"token"];
    __weak typeof(self)WeakSelf = self;
    
    NSDictionary *param = @{
                            @"number":number,
                            @"token":token,
                            @"area_city":@"1",
                            @"area_district":@"1002",
                            @"area_region":@"10062",
                            @"company_name":cnamefield.text,
                            @"company_legal_representative":leNamefield.text,
                            @"company_address":locationfield.text,
                            @"company_license":lincefield.text,
                            @"photo_license":fileImg,
                            @"linkman_name":namefield.text,
                            @"linkman_sex":@"1",
                            @"linkman_phone":tellfield.text,
                            @"linkman_idcard":cardfield.text,
                            @"photo_authorization":companyImg
                            };
    NSString *str = [ReturnSign returnSignWithDic:param];
    NSString *md5String = [NSString stringWithFormat:@"%@%@%@",str,APPKEY_URL,url];
    
    //32位小写
    
    //    NSString *nickname = [nameText.text stringByAddingPercentEscapesUsingEncoding:
    //                          NSUTF8StringEncoding];
    
    md5String = [LCMD5Tool MD5ForLower32Bate:md5String];
    NSMutableDictionary * param2 = [NSMutableDictionary dictionaryWithDictionary:param];
    [param2 setObject:md5String forKey:@"sign"];
    
    [AFHttpTool requestWihtUrl:url params:param2 success:^(id response) {
        
        
        if ([response[@"code"] isEqualToString:@"10000"]) {
            
            WaitReviewViewController *rentVc = [[WaitReviewViewController alloc] init];
            [WeakSelf.navigationController pushViewController:rentVc animated:YES];
            
        }else {
            
            [APPDELEGATE showPro:response[@"message"]];
        }
        
    } failure:^(NSError *err) {
        
        
    }];
    

}

@end
